package com.itheima.reggie.controller;
//套餐管理的相关业务，我们都统一在 SetmealController 中进行统一处理操作。

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**
     * POST
     *
     * 请求路径
     *
     * /setmeal
     *
     * 请求参数
     *
     * json格式数据
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐保存成功");

    }
    /**
     * GET
     *
     * 请求路径
     *
     * /setmeal/page
     *
     * 请求参数
     *
     * ?page=1&pageSize=10&name=xxx
     */
@GetMapping("/page")
@ApiOperation(value = "套餐分页查询接口")
    public R<Page> page(int page,int pageSize,String name){
    /**
     * 1). 构建分页条件对象
     *
     * 2). 构建查询条件对象，如果传递了套餐名称，根据套餐名称模糊查询， 并对结果按修改时间降序排序
     *
     * 3). 执行分页查询
     *
     * 4). 组装数据并返回
     */
    Page<Setmeal> setmealPage = new Page<>(page, pageSize);
    Page<SetmealDto> setmealDtoPage = new Page<>();
    LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
    setmealLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
    setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
    setmealService.page(setmealPage,setmealLambdaQueryWrapper);
    //对象拷贝
    BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
    //对这个records进行单独处理
    List<Setmeal> records = setmealPage.getRecords();
    List<SetmealDto> list = records.stream().map((item)->{
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(item,setmealDto);
        Category category = categoryService.getById(item.getCategoryId());
        if(category!=null){
            setmealDto.setCategoryName(category.getName());
        }
        return setmealDto;
            }).collect(Collectors.toList());
    setmealDtoPage.setRecords(list);
    return R.success(setmealDtoPage);
}
/**
 * DELETE
 *
 * 请求路径
 *
 * /setmeal
 *
 * 请求参数
 *
 * ?ids=1423640210125656065,1423338765002256385
 */
/**
 * 删除套餐  因为删除单个 和批量删除的请求路径方式相同 所以我们写这一个方法就可以了
 * @param ids
 * @return
 */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "套餐删除接口")
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        //因为这里我们删除的时候操作的还是两张表所以需要重新扩展一下方法
        setmealService.removeWithDish(ids);
        return R.success("删除成功");

}
/**
 * 起售停售 批量
 */
@PostMapping("/status/{flag}")
    public R<String> status(@PathVariable Integer flag,@RequestParam List<Long> ids){
    log.info("status:{},ids:{}",flag,ids);
    if(ids==null||ids.size()==0){
        throw new CustomException("参数有误");
    }

    // update setmeal status=-(status-1) where id in (1,2,3);
    LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper = new LambdaUpdateWrapper();//这里一定要记住不再是query了
    setmealLambdaUpdateWrapper.set(Setmeal::getStatus, flag).in(Setmeal::getId, ids);

    setmealService.update(setmealLambdaUpdateWrapper);
//    setmealService.
    return R.success(null);
}
//GET
//	http://localhost:8080/setmeal/1478197193553739777
    /**
     * 修改
     */
    @GetMapping("/{id}")
        public R<SetmealDto> updateShow(@PathVariable Long id){
        //先进行数据的回显
        SetmealDto setmealDto = setmealService.SetmealGetByIdWithDish(id);
        return R.success(setmealDto);


    }
    /**PUT
     http://localhost:8080/setmeal
     * 修改套餐---------
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
       setmealService.updateWithDish(setmealDto);
       return R.success("修改成功");


    }
    /**
     * 根据条件查询套餐数据
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @ApiOperation(value = "套餐条件查询接口")
    public R<List<Setmeal>>  list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);

    }



}
